<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <form class="layui-form" lay-filter="form" style="padding: 20px;">
        <input type="hidden" name="parent_id" value="0">
        
        <div class="layui-form-item">
            <label class="layui-form-label">父级分类</label>
            <div class="layui-input-block">
                <select name="parent_id" lay-filter="parent_id">
                    <option value="0">顶级分类</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="请输入分类名称" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="sort" value="0" lay-verify="number" placeholder="请输入排序" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked>
                <input type="radio" name="status" value="0" title="禁用">
            </div>
        </div>

        <button type="submit" id="save" lay-submit lay-filter="save" style="display: none;">提交</button>
    </form>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['form', 'jquery'], function(){
        var $ = layui.$
        ,form = layui.form;

        // 获取URL参数
        var getUrlParam = function(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]); return null;
        }

        // 如果有父级ID参数，设置父级ID
        var parent_id = getUrlParam('parent_id');
        if(parent_id) {
            $('input[name="parent_id"]').val(parent_id);
        }

        // 加载分类数据
        $.ajax({
            url: '/app/ky_shouyin/admin/goods/category_list',
            type: 'get',
            success: function(res) {
                if (res.code === 0) {
                    var options = '<option value="0">顶级分类</option>';
                    layui.each(res.data, function(index, item) {
                        if (item.status === 1) {
                            var selected = parent_id == item.id ? 'selected' : '';
                            options += '<option value="' + item.id + '" ' + selected + '>' + 
                                     new Array(item.level + 1).join('├ ') + item.name + '</option>';
                        }
                    });
                    $('select[name="parent_id"]').html(options);
                    form.render('select');
                }
            }
        });

        //监听提交
        form.on('submit(save)', function(data){
            return data.field;
        });
    });
    </script>
</body>
</html> 